from datetime import date, timedelta

from rest_framework import generics
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from apps.django_admin.serializer.goodsdays import GoodsSerializer, Userserializer, UserAddSerializer, SpecsSerializer, \
    SPUSerializer
from apps.django_admin.utils import PageNum
from apps.goods.models import GoodsVisitCount, SPUSpecification, SPU
from apps.users.models import User


# 1.用户总数
class UserTotalCountView(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        today = date.today()
        count = User.objects.all().count()
        return Response({
            'count': count,
            'date': today
        })


# 2.用户日增量
class day_increment(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        today = date.today()
        count = User.objects.filter(date_joined__gte=today).count()
        return Response({
            'count': count,
            'date': today
        })


# 3.用户日活跃量
class day_active(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        today = date.today()
        count = User.objects.filter(last_login__gte=today).count()
        return Response({
            'count': count,
            'date': today
        })


# 4.用户日下单人数
class day_orders(APIView):
    permission_classes = [IsAdminUser]

    def get(self, request):
        today = date.today()
        count = User.objects.filter(is_staff=False, orderinfo__create_time__gte=today).distinct().count()
        return Response({
            'count': count,
            'date': today
        })


# 月增加用户统计量
class month_increment(APIView):
    def get(self, request):
        today = date.today()
        start_date = today - timedelta(days=29)
        date_list = []
        for i in range(29, -1, -1):
            index_date = start_date + timedelta(days=i)
            cur_date = start_date + timedelta(days=i + 1)
            count = User.objects.filter(is_staff=False, date_joined__gte=index_date, date_joined__lte=cur_date).count()
            date_list.append({
                'count': count,
                'date': index_date
            })
        return Response(date_list)

class goods_day_views(APIView):
    def get(self, request):
        today = date.today()
        data = GoodsVisitCount.objects.filter(date=today)
        ser = GoodsSerializer(data, many=True)
        return Response(ser.data)


class users(generics.ListCreateAPIView):
    # queryset = User.objects.all()
    serializer_class = Userserializer
    pagination_class = PageNum
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword is '' or keyword is None:
            return User.objects.all()
        else:
            return User.objects.filter(username__contains=keyword)

    def get_serializer_class(self):
        # 请求方式是GET，则是获取用户数据返回UserSerializer
        if self.request.method == 'GET':
            return Userserializer
        else:
            # POST请求，完成保存用户，返回UserAddSerializer
            return UserAddSerializer

class Specs(ModelViewSet):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecsSerializer
    pagination_class = PageNum

class SPUSimpleView(ModelViewSet):
    serializer_class = SPUSerializer
    queryset = SPU.objects.all()

